import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/ext/bool_ext.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/get_util.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/utils/widget_generate_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';

class BatchModificationSingleHeadDialog extends StatefulWidget {
  const BatchModificationSingleHeadDialog({
    Key? key,
  }) : super(key: key);

  @override
  State<BatchModificationSingleHeadDialog> createState() =>
      _BatchModificationSingleHeadDialogState();
}

class _BatchModificationSingleHeadDialogState extends State<BatchModificationSingleHeadDialog> {
  ///商品条码前缀
  final TextEditingController barcodePrefixController = TextEditingController();
  final FocusNode barcodePrefixFocusNode = FocusNode();

  ///商品条码初始值
  final TextEditingController initialValueController = TextEditingController();
  final FocusNode initialValueFocusNode = FocusNode();

  ///商品条码开始位置
  final TextEditingController startSequenceNumberController = TextEditingController();
  final FocusNode startSequenceNumberFocusNode = FocusNode();

  ///商品条码结束位置
  final TextEditingController endSequenceNumberController = TextEditingController();
  final FocusNode endSequenceNumberFocusNode = FocusNode();

  ///重量
  final TextEditingController weightController = TextEditingController();
  final FocusNode weightFocusNode = FocusNode();

  ///服务费
  final TextEditingController serviceChargeController = TextEditingController();
  final FocusNode serviceChargeFocusNode = FocusNode();

  void Function(
    String,
    int,
    int,
    int,
    double,
    double,
  )? callback;

  int maxPosition = 0;

  String barcodePrefix = '';

  int initialValue = -1;

  int startPosition = -1;

  int endPosition = -1;

  double weight = -1.0;

  double serviceCharge = -1.0;

  @override
  void initState() {
    super.initState();
    var arguments = Get.arguments;
    callback = arguments['CallBack'];
    barcodePrefix = arguments['barcodePrefix'] ?? '';
    initialValue = arguments['initialValue'] ?? -1;
    startPosition = arguments['startPosition'] ?? -1;
    endPosition = arguments['endPosition'] ?? -1;
    maxPosition = arguments['maxPosition'] ?? 0;
    if(arguments['weight']!=null && arguments['weight']!=-1){
      weight = (arguments['weight'] as double).numAsUnit();
    }else{
      weight = -1.0;
    }

    serviceCharge = arguments['serviceCharge'] ?? -1.0;

    barcodePrefixController.text = barcodePrefix;
    initialValueController.text = initialValue == -1 ? '' : initialValue.toString();
    startSequenceNumberController.text = startPosition == -1 ? '' : (startPosition + 1).toString();
    endSequenceNumberController.text = endPosition == -1 ? '' : (endPosition + 1).toString();
    weightController.text = weight == -1.0 ? '' : weight.toString();
    serviceChargeController.text = serviceCharge == -1.0 ? '' : serviceCharge.toString();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      backgroundColor: Colors.transparent,
      body: SingleChildScrollView(
        child: Stack(
          children: [
            Column(
              children: [
                SizedBox(height: 188.w / 2 + 0.05.sh),
                Row(
                  children: [
                    SizedBox(width: 0.1.sw),
                    Container(
                      padding: EdgeInsets.only(right: 36.w, left: 36.w, bottom: 36.w),
                      width: 0.8.sw,
                      height: 0.75.sh,
                      decoration: BoxDecoration(
                        color: ThemeUtil.getTheme().colorScheme.secondary,
                        borderRadius: const BorderRadius.all(Radius.circular(15)),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black.withOpacity(0.1), // 阴影的颜色
                            offset: const Offset(0, 0), // 阴影与容器的距离
                            blurRadius: 10.0, // 高斯的标准偏差与盒子的形状卷积。
                            spreadRadius: 0.0, // 在应用模糊之前，框应该膨胀的量。
                          ),
                        ],
                      ),
                      child: Column(
                        children: [
                          SizedBox(height: 36.w),
                          Text('批量修改', style: TextStyles.textBoldBlack36),
                          Gaps.vwGap18,
                          Expanded(
                              child: SingleChildScrollView(
                            physics: const BouncingScrollPhysics(),
                            child: SizedBox(
                              width: double.infinity,
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Gaps.vhGap36,
                                  GenerateWidgetUtil.inputRichText('商品条码前缀'),
                                  Gaps.v_w_Gap24,
                                  GenerateWidgetUtil.inputInfo1(
                                      focusNode: barcodePrefixFocusNode,
                                      keyboardType: TextInputType.text,
                                      hintText: '请输入商品条码前缀',
                                      controller: barcodePrefixController),
                                  Gaps.v_w_Gap24,
                                  GenerateWidgetUtil.inputRichText('商品条码初始化值'),
                                  Gaps.v_w_Gap24,
                                  GenerateWidgetUtil.inputInfo1(
                                      focusNode: initialValueFocusNode,
                                      hintText: '请输入商品条码初始值', controller: initialValueController),
                                  Gaps.v_w_Gap24,
                                  Text('开始序号(不填默认为1)', style: TextStyles.textNormal28),
                                  Gaps.v_w_Gap24,
                                  GenerateWidgetUtil.inputInfo1(
                                      focusNode: startSequenceNumberFocusNode,
                                      hintText: '请输入开始序号',
                                      controller: startSequenceNumberController),
                                  Gaps.v_w_Gap24,
                                  Text('结束序号(不填默认为$maxPosition)', style: TextStyles.textNormal28),
                                  Gaps.v_w_Gap24,
                                  GenerateWidgetUtil.inputInfo1(
                                      focusNode: endSequenceNumberFocusNode,
                                      hintText: '请输入结束序号', controller: endSequenceNumberController),
                                  Gaps.v_w_Gap24,
                                  Text('重量${SPUtil.isJin.unitStrZhWithBracket()}', style: TextStyles.textNormal28),
                                  Gaps.v_w_Gap24,
                                  GenerateWidgetUtil.inputInfo1(
                                      focusNode: weightFocusNode,
                                      hintText: '请输入重量', controller: weightController),
                                  Gaps.v_w_Gap24,
                                  Text('服务费', style: TextStyles.textNormal28),
                                  Gaps.v_w_Gap24,
                                  GenerateWidgetUtil.inputInfo1(
                                      focusNode: serviceChargeFocusNode,
                                      hintText: '请输入服务费', controller: serviceChargeController),
                                ],
                              ),
                            ),
                          )),
                          Row(
                            children: [
                              Expanded(
                                child: GradientButton(
                                  ThemeUtil.getTheme(),
                                  radius: const BorderRadius.only(
                                      topLeft: Radius.circular(50),
                                      bottomLeft: Radius.circular(50)),
                                  colors: ThemeUtil.getTheme().buttonLeft1BgColor(),
                                  height: 80.w,
                                  text: MultiLanguage.cancel.tr,
                                  onPressed: _cancel,
                                ),
                              ),
                              Expanded(
                                child: GradientButton(
                                  ThemeUtil.getTheme(),
                                  radius: const BorderRadius.only(
                                      topRight: Radius.circular(50),
                                      bottomRight: Radius.circular(50)),
                                  height: 80.w,
                                  colors: ThemeUtil.getTheme().buttonRight1BgColor(),
                                  text: MultiLanguage.determine.tr,
                                  onPressed: _determine,
                                ),
                              ),
                            ],
                          )
                        ],
                      ),
                    ),
                    SizedBox(width: 0.1.sw),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  ///确定回调
  void _determine() async {
    if (initialValueController.text.isEmpty) {
      ToastUtil.waring('请输入商品条码初始化');
      return;
    }

    if (!NumUtil.isNumber(initialValueController.text)) {
      ToastUtil.waring('商品条码必须填写纯数字');
      return;
    }

    var temporaryStartPosition = 0;
    if (NumUtil.isNumber(startSequenceNumberController.text)) {
      temporaryStartPosition = int.parse(startSequenceNumberController.text);
      if (temporaryStartPosition > maxPosition) {
        ToastUtil.waring('开始序号不能大于$maxPosition');
        return;
      }
      if (temporaryStartPosition < 1) {
        ToastUtil.waring('开始序号不能小于1');
        return;
      }
      temporaryStartPosition--;
    } else {
      temporaryStartPosition = -1;
    }

    var temporaryEndPosition = 0;
    if (NumUtil.isNumber(endSequenceNumberController.text)) {
      temporaryEndPosition = int.parse(endSequenceNumberController.text);
      if (temporaryEndPosition > (maxPosition)) {
        ToastUtil.waring('结束序号不能大于$maxPosition');
        return;
      }
      if (temporaryEndPosition < 1) {
        ToastUtil.waring('结束序号不能小于1');
        return;
      }
      temporaryEndPosition--;
    } else {
      temporaryEndPosition = -1;
    }

    if (temporaryEndPosition != -1) {
      if (temporaryStartPosition != -1) {
        if (temporaryStartPosition > temporaryEndPosition) {
          ToastUtil.waring('开始序号不能大于结束序号');
          return;
        }
      }
    } else {
      if (temporaryStartPosition != -1) {
        if (temporaryStartPosition > (maxPosition - 1)) {
          ToastUtil.waring('开始序号不能大于结束序号');
          return;
        }
      }
    }

    var temporaryWeight = 0.0;
    if (NumUtil.isNumber(weightController.text)) {
      temporaryWeight = double.parse(weightController.text).numAsUnitSubmit();
    } else {
      temporaryWeight = -1;
    }

    var temporaryServiceCharge = 0.0;
    if (NumUtil.isNumber(serviceChargeController.text)) {
      temporaryServiceCharge = double.parse(serviceChargeController.text);
    } else {
      temporaryServiceCharge = serviceCharge;
    }
    callback?.call(barcodePrefixController.text, int.parse(initialValueController.text),
        temporaryStartPosition, temporaryEndPosition, temporaryWeight, temporaryServiceCharge);
    GetHelper.back();
  }

  _cancel() => GetHelper.back();
}
